
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.UnknownHostException;

public class client {

  
			public static BufferedWriter os = null;
			public static BufferedReader is = null;
			
			public static Socket clientSocket = null;	
  
			public static void main (String [] args ) throws IOException 
			{
		  
					final String serverHost = "localhost";
	    
					Socket clientSocket = null;
		 
					try {   
	        
							clientSocket = new Socket(serverHost, 8081);
		 
							// Create output stream at the client (to send data to the server)
							os = new BufferedWriter(new OutputStreamWriter(clientSocket.getOutputStream()));

							// Input stream at Client (Receive data from the server).
							is = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
	    	 	
							send("SEND");
							
							// Read data sent from the server.
				            // By reading the input stream of the Client Socket.
				     /*       String responseLine;
				            while ((responseLine = is.readLine()) != null) 
							{
				                System.out.println("Server: " + responseLine);
				                if (responseLine.indexOf("OK") != -1) 
								{
				                    break;
				                }
				            }
							
					/*		
						    System.out.println("Stop");

						    DataInputStream dis = new DataInputStream(clientSocket.getInputStream());
							FileOutputStream fos = new FileOutputStream("d:/recevoir_server_to_client.txt");
							byte[] buffer = new byte[4096];
							
							int filesize = 15123; // Send file size in separate msg
							int read = 0;
							int totalRead = 0;
							int remaining = filesize;
							while((read = dis.read(buffer, 0, Math.min(buffer.length, remaining))) > 0) 
							{
								totalRead += read;
								remaining -= read;
								System.out.println("read " + totalRead + " bytes.");
								fos.write(buffer, 0, read);
							}
							
							fos.close();
							dis.close();	
							
						
							
							/*
							
							 String line = in.readLine();
					            while( line != null )
					            {
					                System.out.println( line );
					                line = in.readLine();
					            }
*/
							
						    // BufferedReader stdIn = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

						  
						     /*   while ((userInput = is.readLine()) != null)
						        {
						           
						            System.out.println("Response from server:");
						        	
						        
						        }*/
							
							// receive file
/*							int bytesRead;
						    int current = 0;
						    FileOutputStream fos = null;
						    BufferedOutputStream bos = null;
						   String
						       FILE_TO_RECEIVED = "c:/temp/coco.jpg";  // you may change this, I give a
						                                                            // different name because i don't want to
						                                                            // overwrite the one used by server...

						 int FILE_SIZE = 6022386; // file size temporary hard coded
						                                               // should bigger than the file to be downloaded
							
							
						    byte [] mybytearray  = new byte [FILE_SIZE];
						    InputStream is = clientSocket.getInputStream();
						    fos = new FileOutputStream(FILE_TO_RECEIVED);
						    bos = new BufferedOutputStream(fos);
						    bytesRead = is.read(mybytearray,0,mybytearray.length);
						    current = bytesRead;

						    do {
						         bytesRead = is.read(mybytearray, current, (mybytearray.length-current));
						         if(bytesRead >= 0) current += bytesRead;
						      } while(bytesRead > -1);

						      bos.write(mybytearray, 0 , current);
						      bos.flush();
						      System.out.println("File " + FILE_TO_RECEIVED + " downloaded (" + current + " bytes read)");
				*/			
							
					}
	     
	     
				     catch (UnknownHostException e)
				     {
				           System.err.println("Don't know about host " + serverHost);
				            return;
				     } 
				     catch (IOException e)
				     {
				            System.err.println("Couldn't get I/O for the connection to " + serverHost);
				            return;
				     }
	     
	     
	     try {
	    	 	//os.close();
	    	 	//is.close();
	    	 	clientSocket.close();
	     }
	     
	     catch (UnknownHostException e)
	     {
	            System.err.println("Trying to connect to unknown host: " + e);
	     } 
	     catch (IOException e) 
	     {
	            System.err.println("IOException:  " + e);
	     } 
	     
  }
  
  
		  //simple fonction 
		  private static void send(String message)
		  {
		  	
		  	 try {
		           if (os != null) {
		
		 	    	  os.write(message);
		 	          os.flush();
		 	         System.out.println ("Message envoy au serveur");
		           }
		        }
		        catch (IOException e) { os = null; } 
		  	
		  
		
		  }




	/*	  private static void receive() throws IOException
		{
			
			 // receive file
		
			int filesize=6022386; 
			int bytesRead;
		    int current = 0;
		    byte [] mybytearray  = new byte [filesize];
		  //  InputStream is = clientSocket.getInputStream();
		    
		    File myFile;
		    
		    myFile=new File("img.jpg");
		    ObjectOutputStream oos=new ObjectOutputStream(clientSocket.getOutputStream());
		    oos.writeObject(myFile);
		    
		   // FileOutputStream fos = new FileOutputStream("abc.flv");
		   // BufferedOutputStream bos = new BufferedOutputStream(fos);
		   // bytesRead = is.read(mybytearray,0,mybytearray.length);
		   // current = bytesRead;
		    // thanks to A. Cdiz for the bug fix
		    /*do {
		       bytesRead =
		          is.read(mybytearray, current, (mybytearray.length-current));
		       if(bytesRead >= 0) current += bytesRead;
		    } while(bytesRead > -1);
		    bos.write(mybytearray, 0 , current);
		    bos.flush();
		    long end = System.currentTimeMillis();*/
		   
		/*}*/

}
/*
class StuffThread extends Thread 
{
	  private byte[] data = new byte[255];

	  private Socket socket;

	  public StuffThread(Socket socket) {

	    for (int i = 0; i < data.length; i++)
	      data[i] = (byte) i;
	    this.socket = socket;
	  }

	  public void run() {
	    try {
	      OutputStream out = new BufferedOutputStream(socket.getOutputStream());
	      while (!socket.isClosed()) {
	        out.write(data);
	      }
	      socket.close();
	    } catch (Exception e) {
	    }
	  }
}*/